home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 7 / Apprentice-Release7.iso / Source Code / C / Frameworks / Hsoi's App Shell 1.0a4 / Hsoi's App Shell Source / HASMiscEvents.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-01-28  |  9.8 KB  |  421 lines  |  [TEXT/CWIE]

  1. /*    
  2.     HASMiscEvents.c from Hsoi's App Shell © 1995-1997 John C. Daub.  All rights reserved.
  3.  
  4.     This file contains various and sundry event handlers for events
  5.     that don't need much to handle.
  6.     
  7.     Just a file to keep all those little things in order.  Bigger event
  8.     handlers will get their own files.
  9. */
  10.  
  11. #pragma mark ••• #includes •••
  12.  
  13. #ifndef _WASTE_
  14. #include "WASTE.h"
  15. #endif
  16. #include "HASGlobals.h"
  17. #ifndef __HSOIS_APP_SHELL__
  18. #include "HASMain.h"
  19. #endif
  20. #include "HASDialogs.h"
  21. #include "HASMenus.h"
  22. #include "HASMiscEvents.h"
  23. #include "HASWindows.h"
  24. #include "HASUtilities.h"
  25. #include "HASSoundSpeech.h"
  26.  
  27. #include "WASTE_Objects.h"
  28.  
  29. #ifndef __NOTIFICATION__
  30. #include "Notification.h"
  31. #endif
  32.  
  33. #if HAS_DEBUG
  34. #include "HASUtilTest.h"
  35. #endif
  36.  
  37. #pragma mark -
  38.  
  39.  
  40. /*
  41.  *    This, obviously, is what happens when nothing is happening...for nullEvents.
  42.  */
  43.  
  44. void    HsoiDoIdle( void )
  45. {
  46.     WindowRef    window;    
  47.     
  48.     // see if we have a window up...
  49.     
  50.     window = FrontWindow();
  51.     
  52.     // if we have a window, and it's a text editing window, call some WASTE routines to
  53.     // handle such things as cursor blinking.
  54.  
  55.     if ( (window != nil) &&
  56.          ( HsoiIsDocumentWindow(window) || HsoiIsClipboardWindow(window)) && !gHiliting)
  57.         WEIdle( &gSleepTime, HsoiGetWindowWE( window ) );
  58.     else
  59.     // give up the processor to other applications cause we obviously don't need it
  60.         gSleepTime = MAXLONG;    
  61.     
  62.     return;
  63. }
  64.  
  65. /*
  66.  *    This is called from the event dispatcher when a keyDown event occurs.  Here we decide
  67.  *    just what to do with the key.
  68.  */
  69.  
  70. void    HsoiDoKey( char key, const EventRecord *event )
  71. {
  72.     WindowRef        window;
  73.     SignedByte        keyCode;
  74.     
  75.     window = FrontWindow();
  76.     
  77.     //    do nothing if no window is active
  78.     
  79.     if ( window == nil )
  80.         return;
  81.     
  82.     //    extract virtual key code from event record
  83.     
  84.     keyCode = BSR( BAND(event->message, keyCodeMask ), 8 );
  85.     
  86.     // page movement keys are handled by HsoiDoScrollKey()
  87.     
  88.     switch ( keyCode )
  89.     {
  90.         case keyPgUp:
  91.         case keyPgDn:
  92.         case keyHome:
  93.         case keyEnd:
  94.             HsoiDoScrollKey( keyCode, window );
  95.         break;
  96.         
  97.         default:
  98.             WEKey( key, event->modifiers, HsoiGetWindowWE(window) );
  99.         break;
  100.     }
  101.     
  102.     return;
  103. }
  104.  
  105. /*
  106.  *    This, obviously, takes care of any and all (well, most) update events.
  107.  */
  108.  
  109. void    HsoiDoUpdate( WindowRef window )
  110. {
  111.     GrafPtr        savePort;
  112.     RgnHandle    updateRgn;
  113.     
  114.     // if we have no windows, there's nothing to update!
  115.  
  116.     if ( window == nil )
  117.         return;
  118.     
  119.     // if it's a dialog window, we won't handle updating it here...there are
  120.     // dialog filter routines that handle this.
  121.     
  122.     if ( HsoiIsDialogWindow( window ) )
  123.         return;
  124.     
  125.     // and if we're testing, no updating here.  however, we still have to do
  126.     // the updating "routine" so that stuff in the background (the desktop,
  127.     // Finder windows, stuff from backgrounded apps, etc) gets updated.
  128.     
  129. #if HAS_DEBUG
  130.     if ( HsoiIsTestWindow( window ) )
  131.     {
  132.         GetPort( &savePort );
  133.         SetPortWindowPort( window );
  134.         
  135.         BeginUpdate( window );
  136.         EndUpdate( window );
  137.         
  138.         SetPort( savePort );
  139.         
  140.         return;
  141.     }
  142. #endif
  143.     
  144.     // and now, if it's an app window, let's do something with it.
  145.     
  146.     // save the old drawing port
  147.     
  148.     GetPort( &savePort );
  149.     SetPortWindowPort( window );
  150.     
  151.     // notify everything that we're doing an update.
  152.     
  153.     BeginUpdate( window );
  154.     
  155.     //    BeginUpdate sets the window visRgn to the region to update
  156.     
  157.     updateRgn = GetWindowPort(window)->visRgn;
  158.     if ( !EmptyRgn( updateRgn ) )    // if it's not an empty region, let's update it!
  159.     {
  160.         //    erase update region
  161.         EraseRgn( updateRgn );
  162.         
  163.         //    draw scroll bars
  164.         UpdateControls( window, updateRgn );
  165.         
  166.         //    draw grow icon
  167.         HsoiMyDrawGrowIcon( window, false );
  168.         
  169.         //    draw text
  170.         WEUpdate( updateRgn, HsoiGetWindowWE(window) );
  171.         
  172.         // and if it's the clipboard window, gotta do a couple extra things to update it
  173.         
  174.         if ( HsoiIsClipboardWindow( window ) )
  175.             HsoiDoClipboardUpdate( window );
  176.     }
  177.  
  178.     // tell everything we're done updating
  179.     
  180.     EndUpdate( window );
  181.     
  182.     // restore the old graphics port
  183.     
  184.     SetPort( savePort );
  185.     
  186.     return;
  187. }
  188.  
  189. /*
  190.  *    This take care of activation (osEvt's).  stuff to make the windows all look right and
  191.  *    proper when in the foreground or background.
  192.  */
  193.  
  194. void    HsoiDoActivate( Boolean activFlag, WindowRef window )
  195. {
  196.     DocumentHandle    hDocument;
  197.     WEReference        we;
  198.     GrafPtr            savePort;
  199.     ControlPartCode    barHilite;
  200.     Rect            barRect;
  201.  
  202.     // if there aren't any windows, nothing to do here...
  203.  
  204.     if (window == nil)
  205.         return;
  206.         
  207.     // if there's a dialog, just make sure the menus are proper, the toolbox tends to
  208.     // handle the rest via our dialog filter routines.
  209.     
  210.     if ( HsoiIsDialogWindow( window ) )
  211.     {
  212.         HsoiAdjustMenus();
  213.         return;
  214.     }
  215.     
  216.     // if the test window, just make the menus look right
  217.     
  218. #if HAS_DEBUG
  219.  
  220.     if ( HsoiIsTestWindow( window ) )
  221.     {
  222.         HsoiAdjustMenus();
  223.         return;
  224.     }
  225.  
  226. #endif
  227.     
  228.     // now take care of the rest of the situations...
  229.     
  230.     hDocument = HsoiGetWindowDocument(window);
  231.     we = (*hDocument)->we;
  232.     
  233.     //     set up the port
  234.     
  235.     GetPort( &savePort );
  236.     SetPortWindowPort( window );
  237.  
  238.     // activate or deactivate the text (and any other relevant stuff) depending on just
  239.     // what we're doing here...
  240.     
  241.  
  242.     if ( activFlag )
  243.     {
  244.         WEActivate( we );
  245.         barHilite = kControlNoPart;
  246.     }
  247.     else
  248.     {
  249.         WEDeactivate( we );
  250.         barHilite = kControlDisabledPart;
  251.     }
  252.     
  253.     
  254.     //    redraw the grow icon (and validate it's rect)
  255.     
  256.     HsoiMyDrawGrowIcon( window, true );
  257.     
  258.     //    redraw the scroll bars with the new highlighting (and validate their rects)
  259.     
  260.     //    first, the vertical scroll bar
  261.     
  262.     HiliteControl( ((*hDocument)->scrollBars).v, barHilite );
  263.     HsoiCalcScrollBarRect(window, v, &barRect );
  264.     ValidRect( &barRect );
  265.     
  266.     //    now the horizontal scroll bar
  267.  
  268.     HiliteControl( ((*hDocument)->scrollBars).h, barHilite );
  269.     HsoiCalcScrollBarRect( window, h, &barRect );
  270.     ValidRect( &barRect );
  271.  
  272.     //    dim or undim text-related menus
  273.  
  274.     HsoiAdjustMenus();
  275.     
  276.     // restore the old graphics port..
  277.     
  278.     SetPort( savePort );
  279.     
  280.     return;
  281. }
  282.  
  283.  
  284. #pragma mark -
  285. #pragma mark ••• Notification •••
  286.  
  287.  
  288. // Following here are a few routines to handle Notification Manager stuff.  It's in this
  289. // file cause it deals with suspend and resume events.
  290.  
  291. /*
  292.  *    This function is called when we want to post a notification message to the user.
  293.  *    it sticks the message in the queue.
  294.  */
  295.  
  296. void    HsoiPostNMMessage( void )
  297. {
  298.     OSErr        err;
  299.     
  300.     // tho probably not necessary to check for this, can't hurt to do
  301.     // if they don't have the notification manager on the computer, obviously we can't
  302.     // post a notification message...plus, you shouldn't post a notification request unless
  303.     // the program is in the background...if it's in the foreground, they're working with it
  304.     // already, use a dialog or something else...Notificaiton Manger stuff will still work
  305.     // regardless if the app is front or backgrounded, but it's just good programming
  306.     // practice to use the Notificatino Manager only when you need to alert the user to
  307.     // something that isn't in their face.
  308.         
  309.     if ( !gInBackground || !gHasNotification )
  310.         return;
  311.  
  312.     // set things that need to be set
  313.     
  314.     gNotifyRec.qType = nmType;        // identifies NMRec as a notification request queue element
  315.     gNotifyRec.nmResp = 0L;            // no response procedure, we'll remove stuff in the resume event
  316.     gNotifyRec.nmRefCon = 0L;        // nothing exciting to store in the refcon
  317.     
  318.     // set things according to the user's preferences...
  319.     
  320.     if ( gMyPrefs.useIconNM )
  321.     {
  322.         gNotifyRec.nmIcon = GetResource( TYPE_SMALL_ICON, rNotifyIcon );
  323.         HNoPurge( gNotifyRec.nmIcon );
  324.     }
  325.     else
  326.         gNotifyRec.nmIcon = nil;
  327.         
  328.     gNotifyRec.nmMark = gMyPrefs.useDiamondNM;
  329.     
  330.     if ( gMyPrefs.useSoundNM )
  331.     {
  332.         gNotifyRec.nmSound = GetResource( TYPE_SOUND, rAboutSoundUhh );    // or could set sound to -1L to play the sys beep
  333.         HNoPurge( gNotifyRec.nmSound );
  334.     }
  335.     else
  336.         gNotifyRec.nmSound = nil;
  337.     
  338.     if ( gMyPrefs.useAlertNM )
  339.     {
  340.         GetIndString( gNotifyStr, rNotificationManagerStrings, strNMBringToFront );
  341.         gNotifyRec.nmStr = gNotifyStr;
  342.     }
  343.     else
  344.         gNotifyRec.nmStr = nil;
  345.     
  346.     // tho this is most likely unnecessary to do (notification requests should only
  347.     // get posted if your application is in the background, and if our app is in
  348.     // the background, we've already stopped any playing sound(s) upon the suspend
  349.     // event), i still want to  have a check in here for it.  it can't hurt that
  350.     // much, and who knows...maybe some strange scripting or other AppleEvent
  351.     // might cause us to need this.
  352.     
  353.     // i just want a sound to stop playing cause we'll be using notification
  354.     // sounds, the alert can use a sound, etc....don't want to mix and mush up sounds
  355.     
  356.     if ( SoundIsPlaying() )
  357.         StopCurrentSound();
  358.     
  359.     // now let's actually stick the notification request into the queue
  360.     
  361.     err = NMInstall( &gNotifyRec );
  362.     
  363.     if ( err != noErr )
  364.         SysBeep( 5 ); // lame error handling, but for now....NMInstall should almost never fail
  365.     
  366.     // and set a global boolean for us to keep track of stuff...
  367.     
  368.     gNotifying = true;
  369.  
  370.     return;
  371. }
  372.  
  373.  
  374. /*
  375.  *    The above function puts a message in the queue, this removes it (after the user takes
  376.  *    care of returning to the program)
  377.  */
  378.  
  379. void    HsoiRemoveNMMessage( void )
  380. {
  381.     // if the computer doesn't even have the Notificaiton Manager, forget it.
  382.     
  383.     if ( !gHasNotification )
  384.         return;
  385.     
  386.     // if we are notifying them (there is a message in the queue)...
  387.     
  388.     if ( gNotifying )
  389.     {
  390.         // ... remove it!
  391.         
  392.         NMRemove( &gNotifyRec );
  393.         
  394.         // if we had to grab some resources to make our notification request, let's
  395.         // make sure to free up their memory.
  396.         
  397.         if ( gNotifyRec.nmIcon )
  398.             HsoiForgetResource( &gNotifyRec.nmIcon );
  399.         
  400.         if ( gNotifyRec.nmSound )
  401.             HsoiForgetResource( &gNotifyRec.nmSound );
  402.         
  403.         // set our boolean back to an appropriate value...
  404.         
  405.         gNotifying = false;
  406.         
  407.         // throw up a quicky alert to say just why i notified you in the first place...
  408.         // (you probably won't use something like this normally, I'm just doing this
  409.         // as a lame way of showing you a way to impliment your own "response" procedure)
  410.         
  411.         // make sure the cursor is kosher
  412.         
  413.         InitCursor();
  414.         
  415.         // throw up the alert (i just used NoteAlert() cause it's simple and easy)
  416.         
  417.         NoteAlert( rNMRespAlert, HsoiGetMyStandardDialogFilter() );
  418.     }
  419.     
  420.     return;
  421. }